package com.example.administrator.nl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.nl.R;
import com.example.administrator.nl.base.BaseActivity;
import com.example.administrator.nl.util.ActivityManager;
import com.example.administrator.nl.util.CountTimerUtils;
import com.example.administrator.nl.util.ToastUtil;
import com.leaf.library.StatusBarUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class RegisterActivity extends BaseActivity {
    private int REQUEST = 0x00001;

    // 返回按钮
    @BindView(R.id.iv_finish)
    ImageView ivFinish;

    // 页面标题
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl)
    RelativeLayout rl;

    // 手机号
    @BindView(R.id.et_phone_register)
    EditText etPhoneRegister;

    // 验证码输入框
    @BindView(R.id.et_verification_code_register)
    EditText etVerificationCodeRegister;

    // 获取验证码短信按钮
    @BindView(R.id.tv_verification_code_register)
    TextView tvVerificationCodeRegister;

    // 密码输入框
    @BindView(R.id.et_pass_register)
    EditText etPassRegister;

    // 再次输入密码框
    @BindView(R.id.et_s_pass_register)
    EditText etSPassRegister;

    // 用户协议复选框
    @BindView(R.id.check_clause_register)
    CheckBox checkClauseRegister;

    // 用户协议
    @BindView(R.id.tv_clause_register)
    TextView tvClauseRegister;

    // 注册按钮
    @BindView(R.id.tv_register_register)
    TextView tvRegisterRegister;

    // 验证码倒计时器
    CountTimerUtils mCountTimerUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void init() {
        StatusBarUtil.setColor(RegisterActivity.this, getResources().getColor(R.color.color_white_ffffff));
        tvTitle.setText("账号注册");

    }

    @Override
    public int getContentView() {
        return R.layout.activity_register;
    }

    @OnClick({R.id.iv_finish, R.id.tv_verification_code_register, R.id.check_clause_register, R.id.tv_register_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            // 返回到上个页面
            case R.id.iv_finish:
                finish();
                break;

            // 获取验证码
            case R.id.tv_verification_code_register:
                mCountTimerUtils = new CountTimerUtils(60000, 1000, tvVerificationCodeRegister);
                mCountTimerUtils.start();
                break;

            // 注册按钮
            case R.id.tv_register_register:
                // 是否输入手机号
                if (!TextUtils.isEmpty(etPhoneRegister.getText())) {
                    // 是否输入验证码
                    if (!TextUtils.isEmpty(etVerificationCodeRegister.getText())) {
                        // 是否输入密码
                        if (!TextUtils.isEmpty(etPassRegister.getText())) {
                            // 是否输入密码
                            if (!TextUtils.isEmpty(etSPassRegister.getText())) {
                                // 两次密码是否都正确
                                if (etPassRegister.getText().toString().trim().equals(etSPassRegister.getText().toString().trim())) {
                                    if (checkClauseRegister.isChecked()) {
                                        // 注册成功，跳转到登录页面
                                        Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                                        intent.putExtra("phone", etPhoneRegister.getText().toString().trim());
                                        startActivity(intent);
                                        finish();
                                    } else {
                                        ToastUtil.ToastShort("阅读用户协议");
                                    }
                                } else {
                                    ToastUtil.ToastShort("两次密码不相同");
                                }

                            } else {
                                ToastUtil.ToastShort("请输入密码");
                            }
                        } else {
                            ToastUtil.ToastShort("请输入密码");
                        }
                    } else {
                        ToastUtil.ToastShort("请输入验证码");
                    }
                } else {
                    ToastUtil.ToastShort("请输入手机号");
                }
                break;
        }
    }

    @Override
    public boolean isFinishing() {
        if (super.isFinishing()) {
            // 取消验证码倒计时
            if (null != mCountTimerUtils) {
                mCountTimerUtils.cancel();
            }

            return true;
        }
        return false;

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
